<template>
    <div class="edit">
        <el-form :model="form" :rules="rules" label-width="80px" ref="form">
            <el-form-item label="班级名" prop="name">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="年级" prop="grade">
                <el-input-number v-model="form.grade" controls-position="right" :min="2017"
                    :max="2025"></el-input-number>
            </el-form-item>
            <el-form-item label="所属专业" prop="majorId">
                <el-select v-model="form.majorId" placeholder="请选择专业">
                    <el-option v-for="item in options" :key="item.index" :label="item.name"
                        :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import {
    majorNames,
    classAdd,
    classEdit,
    getClass,
} from "@/api/class.js"
export default {
    props: ['type', 'id'],
    data() {
        return {
            options: [],
            form: {
                id: -1,
                name: '',
                grade: 2019,
                majorId: '',
            },
            rules: {
                name: [
                    { require: true, message: '请输入专业名', trigger: 'blur' },
                ],
                grade: [
                    { require: true, type: 'number', message: '请输入年级', trigger: 'blur' },
                ],
                majorId: [
                    { require: true, type: 'number', message: '请选择系', trigger: 'blur' },
                ],
            },
        }
    },
    methods: {
        async getOptions() {
            let ret = await majorNames()
            this.options = ret.data
        },
        async getName(id) {
            if (this.type === 'edit') {
                let ret = await getClass(id);
                this.form = ret.data
            }
        },
        submit() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.type === 'add') {
                        classAdd(this.form).then(res => {
                            if (res.code === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '添加成功',
                                    duration: 1000,
                                });
                                this.$emit('done')
                            }
                        })
                    } else if (this.type === 'edit') {
                        classEdit(this.form).then(res => {
                            if (res.code === 0) {
                                this.$message({
                                    type: 'success',
                                    message: '修改成功',
                                    duration: 1000,
                                });
                                this.$emit('done')
                            }
                        })
                    }
                }
            });
        },
        cancel() {
            this.$emit('cancel')
        },
    },
    watch: {
        id: {
            handler(newVal, oldVal) {
                this.getName(newVal)
            },
            immediate: true
        }
    },
    mounted() {
        this.getOptions()
    }
}
</script>